package moniCA::LOGFILE::PARSE;

#################################################################
# $LastChangedDate: 2013-07-09 06:18:50 +0000 (Tue, 09 Jul 2013) $ 
# $LastChangedRevision: 12 $ 
# $LastChangedBy: aswod_83@gmx.net $
#
# (C) 2010 Monica DevTEAM
# This program is free software; you can redistribute it and/or modify 
# it under the terms of the GNU General Public License as published by 
# the Free Software Foundation; either version 3 of the License, or 
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but 
# WITHOUT ANY WARRANTY; without even the implied warranty of 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License 
# along with this program; if not, see <http://www.gnu.org/licenses/>.
#################################################################
use strict;
use base qw /moniCA/; 
use Data::Dumper;
use XML::Simple;

#-------------------------------------------------------------------------------
sub find_err {
#-------------------------------------------------------------------------------
# Description:  
# Input Parms:
# Output Parms: None
#-------------------------------------------------------------------------------	
	my $self        	=	shift;
	my $log				=	shift;
	my $cnf         	=	$self->GetCNF();
	my $logger      	=	$self->GetLogger();
	my $statefile   	=	$self->TmpDir() . '/' . $self->Plug() . '.xml';
	my $ostat			=	XMLin($statefile,ForceArray => 1) if (-s $statefile );
	my %nstat;
	my $last_log_pos	=	$ostat->{last_log_pos} || 0;
	my @ERRORS          =	();
	my $current_pos     =	0;
	my $todo            =	0;

	if ( -r $log ) {
		$current_pos 	=	-s $log;
		$last_log_pos 	=	0 if (  $last_log_pos > $current_pos  );
		$todo 			=	$current_pos - $last_log_pos;
 	} else {
		$todo 			=	0;
		$logger->warn("$log nicht vorhanden oder lesbar !\n");
	}

	$logger->debug("start position: $last_log_pos / end position: $current_pos");
		
	if ( $todo ) {
		open( LOG, $log );
		seek LOG, $last_log_pos, 0;
		my $line;
		my @whitelist= map { $cnf->{whitelist}->{$_} } grep { /WL/ } sort keys %{$cnf->{whitelist}};
		my @blacklist= map { $cnf->{blacklist}->{$_} } grep { /BL/ } sort keys %{$cnf->{blacklist}};
		while ($line = <LOG>) {
			foreach (@blacklist) {
				push @ERRORS, $line if ($line =~ m/$_/i &&
                                          ! $self->_checkwhitelist($line, \@whitelist));
			}                                       
		}
		close LOG;
		if ( @ERRORS > 0 ) {
			chomp(@ERRORS);
			for my $line (  @ERRORS ) {
				$logger->error($line);								
			}
		}
	}
                
	$nstat{last_log_pos}=$current_pos;
	XMLout(\%nstat,outputfile => $statefile ,NoSort=>0);
	return $self;
}

#-------------------------------------------------------------------------------
sub _checkwhitelist {
#-------------------------------------------------------------------------------
# Description:  
# Input Parms:
# Output Parms: None
#-------------------------------------------------------------------------------		
	my $self        = shift;
	my $line        = shift;
 	my $whitelist   = shift;
	my $counter     = 0;
	for ( @{$whitelist} ) {
		if ( $line =~ m/\Q$_/i ) {
			$counter++;
		}
	}
	return $counter;
}

1;
